<?php
/**
 * @Author:      lyt
 * @DateTime:    2016-03-15 09:27:45
 * @Description: 用于 发票的 接口
 */
class ScsInvoiceController extends BaseController {

  /**
   * @Author:      lyt
   * @DateTime:    2016-03-15 09:28:13
   * @Description: Description
   */
    public  function invoiceList(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "order_id"=>'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $order=Orders::find(Input::get('order_id'));
            if(is_null($order)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($order->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            $invoicelist=InvoiceRecords::where("order_id",$order->order_id)->where("status",1)->get();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>sizeof($invoicelist),"orders"=>$invoicelist));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "invoiceList", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    public  function create(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "order_id"=>'required',
                "title"=>'max:100',
                "invoice_no"=>'max:100',
                "type"=>'required',
                "amount"=>'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $order=Orders::find(Input::get('order_id'));
            if(is_null($order)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($order->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if(!in_array($order->status,Orders::$invoice)){//订单状态是正确
                $response = Lang::get("scs_error.order_status_error_40027");
                break;
            }
            $invoice_amount=InvoiceRecords::where("order_id",$order->order_id)->where("status",1)->sum('amount');
            $amount=Input::get("amount")*100;
            if($invoice_amount+$amount>$order->payable_amount){
                $response = Lang::get("scs_error.invoice_amount_error_80000");
                break;
            }
            $iv=new InvoiceRecords();
            $iv->user_id=$order->user_id;
            $iv->order_id=$order->order_id;
            $iv->title=Input::get("title");
            $iv->invoice_no=Input::get("invoice_no");
            $iv->type=Input::get("type");
            $iv->status=1;
            $iv->amount=$amount;
            $iv->address="";
            $iv->create_by=$sc_user->id;
            $iv->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$iv->id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "create", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zy
     * @DateTime:    2017-08-15 
     * @Description: 返回用户最新使用过的发票抬头信息
     */
      public  function userInvoiceTitle(){
          $validator = Validator::make(Input::all(),
              array(
                  'token' => 'required',
                  "user_id"=>'required',
              )
          );
          $response = array('code' => 0, 'msg' => 'ok');
          do{
              if($validator->fails()) {
                  $response = Lang::get("scs_error.args_error_10000");
                  $response["result"]=$validator->messages()->all();
                  break;
              }
              $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
              if(is_null($sc_user)){
                  break;
              }
              
              $invoice=InvoiceRecords::select('title')->where('user_id',Input::get("user_id"))->orderBy('created_at', 'desc')->first();
              $title = "";
              if ($invoice) {
                  $title = $invoice->title;
              }

              $response = array('code' => 0, 'msg' => 'ok',"title"=>$title);
          }while(false);
          Log::info("info", array("system" => "scs" ,"event" => "userInvoiceTitle", 'input' =>Input::all() ,"output" => $response));
          return json_encode($response);
      }
     
}